from django.db import models
import time


# BOM单
class FaKuyouBom(models.Model):
    id = models.AutoField(primary_key=True)
    purchase_code = models.CharField(
        max_length=50,
        null=True,
        blank=True,
        verbose_name='单据编号',
        db_index=True  # 创建索引
    )
    name = models.CharField(
        max_length=255,
        null=True,
        blank=True,
        verbose_name='清单名称'
    )
    goods_name = models.CharField(
        max_length=255,
        null=True,
        blank=True,
        verbose_name='商品名称'
    )
    goods_code = models.CharField(
        max_length=100,
        null=True,
        blank=True,
        verbose_name='商品编号'
    )
    number = models.DecimalField(
        max_digits=15,
        decimal_places=4,
        null=True,
        blank=True,
        verbose_name='数量',
        default=0
    )
    all_money = models.FloatField(
        null=True,
        blank=True,
        verbose_name='金额',
        default=0
    )
    wages = models.DecimalField(
        max_digits=15,
        decimal_places=4,
        default=0.0000,
        verbose_name='计件工资'
    )
    createtime = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='创建时间',
        default=int(time.time())
    )
    updatetime = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='更新时间',
        default=int(time.time())
    )

    class Meta:
        db_table = 'fa_kuyou_bom'
        verbose_name = 'BOM清单'
        verbose_name_plural = 'BOM清单'
        indexes = [
            models.Index(fields=['goods_code']),  # 商品编号也常作为查询条件
        ]

    def __str__(self):
        return f"{self.purchase_code or '无编号'} - {self.name or '未命名清单'}"

    @property
    def total_cost(self):
        """计算总成本（材料+工资）"""
        material_cost = float(self.all_money) if self.all_money else 0
        labor_cost = float(self.wages) if self.wages else 0
        return material_cost + labor_cost


# BOM商品单
class FaKuyouBomInfo(models.Model):
    id = models.AutoField(primary_key=True)
    bom_id = models.IntegerField(null=True, blank=True, verbose_name='清单ID')
    procedure_id = models.IntegerField(null=True, blank=True, verbose_name='工序ID')
    goods_id = models.IntegerField(null=True, blank=True, verbose_name='商品ID')
    goods_code = models.CharField(max_length=255, null=True, blank=True, verbose_name='商品编号')
    goods_name = models.CharField(max_length=255, null=True, blank=True, verbose_name='商品名称')
    goods_note = models.CharField(max_length=255, null=True, blank=True, verbose_name='商品规格型号')
    unit_id = models.IntegerField(null=True, blank=True, verbose_name='单位ID')
    default_unit = models.IntegerField(null=True, blank=True, verbose_name='默认单位')
    purchase = models.DecimalField(
        max_digits=15,
        decimal_places=4,
        default=0.0000,
        verbose_name='进价'
    )
    number = models.DecimalField(
        max_digits=15,
        decimal_places=4,
        default=0.0000,
        verbose_name='数量'
    )
    basic_nmber = models.DecimalField(
        max_digits=15,
        decimal_places=4,
        null=True,
        blank=True,
        verbose_name='基本数量'
    )
    money = models.DecimalField(
        max_digits=15,
        decimal_places=4,
        null=True,
        blank=True,
        verbose_name='金额'
    )
    remark = models.TextField(null=True, blank=True, verbose_name='备注')
    createtime = models.IntegerField(null=True, blank=True, verbose_name='创建时间', default=time.time())
    updatetime = models.IntegerField(null=True, blank=True, verbose_name='更新时间', default=time.time())

    class Meta:
        db_table = 'fa_kuyou_bom_info'
        verbose_name = 'BOM商品'
        verbose_name_plural = 'BOM商品'
        indexes = [
            models.Index(fields=['bom_id']),
            models.Index(fields=['goods_id']),
            models.Index(fields=['procedure_id']),
        ]

    def __str__(self):
        return f"{self.goods_name or '未命名商品'} (BOM:{self.bom_id})"


# BOM工序单
class FaKuyouBomProcedure(models.Model):
    id = models.AutoField(primary_key=True)
    bom_id = models.IntegerField(null=True, blank=True, verbose_name='清单ID')
    purchase_code = models.CharField(
        max_length=255,
        null=True,
        blank=True,
        verbose_name='工序编号'
    )
    name = models.CharField(
        max_length=255,
        null=True,
        blank=True,
        verbose_name='工序名称'
    )
    workshop_id = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='车间ID'
    )
    wages = models.DecimalField(
        max_digits=15,
        decimal_places=4,
        default=0.0000,
        verbose_name='单位工资'
    )
    hours = models.FloatField(
        default=0.00,
        verbose_name='标准工时'
    )
    remark = models.TextField(
        null=True,
        blank=True,
        verbose_name='备注'
    )
    createtime = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='创建时间',
        default=time.time()
    )
    updatetime = models.IntegerField(
        null=True,
        blank=True,
        verbose_name='更新时间',
        default=time.time()
    )

    class Meta:
        db_table = 'fa_kuyou_bom_procedure'
        verbose_name = 'BOM工序'
        verbose_name_plural = 'BOM工序'
        indexes = [
            models.Index(fields=['bom_id']),
            models.Index(fields=['workshop_id']),
            models.Index(fields=['purchase_code']),
        ]

    def __str__(self):
        return f"{self.purchase_code or ''} {self.name or '未命名工序'} (BOM:{self.bom_id})"

    def save(self, *args, **kwargs):
        # 自动更新时间戳
        now = int(time.time())
        if not self.createtime:
            self.createtime = now
        self.updatetime = now

        super().save(*args, **kwargs)

    @property
    def total_wages(self):
        """计算总工资"""
        return float(self.wages) * self.hours if self.wages and self.hours else 0.0
